package com.ioudi.digu.memcached;

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;

public class MemcachedClient
{
	public final static String ID_USER_DB = "IdUserDB";
	
	public final static String ID_USER_MESSAGE_SELECTOR = "IdUserMS";
	
	public final static String ID_MESSAGEID_MESSAGE = "messageId2Message";
	
	public final static String ID_USER_FRIENDSHIP_MAP = "userFriendship";
	
	protected static MemCachedClient mc = new MemCachedClient();
	
	public MemcachedClient(String[] servers, Integer[] weights)
	{
		init(servers, weights);
	}
	
	private void init(String[] servers, Integer[] weights)
	{
		// grab an instance of our connection pool
		SockIOPool pool = SockIOPool.getInstance();

		// set the servers and the weights
		pool.setServers( servers );
		pool.setWeights( weights );

		// set some basic pool settings
		// 5 initial, 5 min, and 250 max conns
		// and set the max idle time for a conn
		// to 6 hours
		pool.setInitConn( 5 );
		pool.setMinConn( 5 );
		pool.setMaxConn( 250 );
		pool.setMaxIdle( 1000 * 60 * 60 * 6 );

		// set the sleep for the maint thread
		// it will wake up every x seconds and
		// maintain the pool size
		pool.setMaintSleep( 30 );

		// set some TCP settings
		// disable nagle
		// set the read timeout to 3 secs
		// and don't set a connect timeout
		pool.setNagle( false );
		pool.setSocketTO( 3000 );
		pool.setSocketConnectTO( 0 );

		// initialize the connection pool
		pool.initialize();


		// lets set some compression on for the client
		// compress anything larger than 64k
		mc.setCompressEnable( true );
		mc.setCompressThreshold( 64 * 1024 );
		
		
	}
	
	public boolean set(String key, Object value)
	{
		return mc.set(key, value);
	}
	
	public Object get(String key)
	{
		return mc.get(key);
	}
}
